<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Vendor extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper('bantuan_helper');
		$this->load->model('Produk_model');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
			redirect('backend');
		endif;
	}

	function index()
	{
	
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/vendor/index';
		$config['total_rows'] = count($this->Produk_model->getBrands('all',FALSE,FALSE,FALSE));
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Brand';
		$data['template'] = 'vendor/index';
		$data['res'] = $this->Produk_model->getBrands('all',FALSE,$config['per_page'],$offset);
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}

	function add()
	{
		$this->form_validation->set_rules('nama_brand', 'Brand Name','required');
		$this->form_validation->set_rules('brand_desc', 'Brand Desc','required');
		if ($this->form_validation->run() == FALSE)
		{
			$data['title'] = 'Brand';
			$data['template'] = 'vendor/add';
			$this->load->view('backend/index',$data);
		}
		else
		{
			$data = array(
				'nama_brand'=>$this->input->post('nama_brand'),
				'brand_site'=>$this->input->post('brand_site'),
				'deskripsi'=>$this->input->post('brand_desc'),
				'caption'=>$this->input->post('caption'),
				'brand_homepage'=>$this->input->post('brand_homepage'),
				'brand_slidepage'=>$this->input->post('brand_slidepage'),
				'brand_status'=>$this->input->post('brand_status'),
				);
			if((!empty($_FILES['image_file']['tmp_name'])) && ($_FILES['image_file']['tmp_name']!="")):
				$this->db->insert('tb_brand',$data);
				$id = $this->db->insert_id();
				$this->uploadImage($data,'add',$id);
			else:
				$this->db->insert('tb_brand',$data);
			endif;
			$this->session->set_flashdata('message_type','success');
			$this->session->set_flashdata('message', '<div id="kotak" class="success">Data berhasil disimpan</div>');
			redirect('backend/vendor');
		}	
	}

	function edit()
	{
		$this->form_validation->set_rules('nama_brand', 'Nama Brand','required');
		$this->form_validation->set_rules('brand_desc', 'Brand Desc','required');
		$id = $this->uri->segment(4);
		if ($this->form_validation->run() == FALSE):
			$data['title'] = 'Brand';
			$data['template'] = 'vendor/edit';
			$data['row'] = $this->db->where('brand_id',$id)->get('tb_brand')->row();
			$this->load->view('backend/index',$data);
		
		else :
			$data = array(
				'nama_brand'=>$this->input->post('nama_brand'),
				'brand_site'=>$this->input->post('brand_site'),
				'deskripsi'=>$this->input->post('brand_desc'),
				'caption'=>$this->input->post('caption'),
				'brand_homepage'=>$this->input->post('brand_homepage'),
				'brand_slidepage'=>$this->input->post('brand_slidepage'),
				'brand_status'=>$this->input->post('brand_status'),
				);
			if((!empty($_FILES['image_file']['tmp_name'])) && ($_FILES['image_file']['tmp_name']!="")):
				$this->uploadImage($data,'edit',$id);
			endif;
			$this->db->where('brand_id',$id)->update('tb_brand',$data);
			$this->session->set_flashdata('message_type','success');
			$this->session->set_flashdata('message', '<div id="kotak" class="success">Data berhasil disimpan</div>');
			redirect('backend/vendor');
		endif;
	}
	
	function uploadImage($data,$param=FALSE,$brand_id=FALSE)
	{
		$this->load->helper('file');
		$absolutePath = $this->config->item("absolutePathFile").'brand_slide/';
		// config untuk upload
		$config['upload_path'] = $this->config->item("upload_path_temp");
		$config['allowed_types'] = 'jpg|jpeg';
		$config['max_size'] = $this->config->item("max_size");
		$config['max_width'] = $this->config->item("max_width");
		$config['max_height'] = $this->config->item("max_height");
		$config['remove_spaces'] = $this->config->item("remove_spaces");

		// inisialisasi library upload
		$this->load->library('upload', $config);
		
		// uploading
		if (!$this->upload->do_upload('image_file')):
			print_r($this->upload->display_errors());
		else:
			$uploaded_photo = array('upload_photo' => $this->upload->data());
			$file_uploaded = $uploaded_photo['upload_photo']['file_name'];
			$file_ext = $uploaded_photo['upload_photo']['file_ext'];
			$randName = 'slide_'.mt_rand(100000,999999);
			$newName = $randName;
			while(file_exists($config['upload_path'].$newName)):
				$randName = 'slide_'.mt_rand(100000,999999);
				$newName = $randName;
			endwhile;
			$fileDate = date("YmdHis");
			$newFileName = $newName.'_'.$fileDate.$file_ext;
			if(!file_exists($config['upload_path'].$newFileName)):
				rename($config['upload_path'].$file_uploaded, $config['upload_path'].$newFileName);
			endif;

			// config untuk resize
			$large_resize_width = 600;
			$large_resize_height = 420;

			require_once FCPATH . APPPATH . 'libraries/phpthumb/ThumbLib.inc.php';
			
			$filename_noext = $newName.'_'.$fileDate;
			$thumb = PhpThumbFactory::create($config['upload_path'].$newFileName);
			$thumb->adaptiveResize($large_resize_width, $large_resize_height);
			$thumb->save($absolutePath . $filename_noext . '_'.$large_resize_width.'x'.$large_resize_height.'.jpg', 'jpg');
			$brand_img_url_slider = $filename_noext . '_'.$large_resize_width.'x'.$large_resize_height.'.jpg';

			unlink($config['upload_path'].$newFileName);
			if($param=='edit'):
				$cekData = $this->db->where('brand_id',$brand_id)
						->get('tb_brand')
						->row();
				if(count($cekData)>0):
					if(!empty($cekData->brand_img_url_slider) && file_exists($absolutePath.$cekData->brand_img_url_slider)):
						unlink($absolutePath.$cekData->brand_img_url_slider);
					endif;
				endif;
			endif;
			$data_image = array(
				'brand_img_url_slider' => $brand_img_url_slider
			);
			$this->db->where('brand_id',$brand_id)->update('tb_brand',$data_image);
		endif;
	}

	function delete()
	{
		$id = $this->uri->segment(4);
		$this->Produk_model->deleteVendor($id);
		$this->session->set_flashdata('message_type','<div id="kotak" class="success">Satu data berhasil dihapus</div>');
		redirect('backend/vendor','refresh');
	
	}
	
	function ajax_bulk_action()
	{
		foreach($_POST["vendor_id"] as $id):
			$this->db->where('vendor_id',$id);
			$this->db->delete('tb_vendor');
		endforeach;
		
	}
}